# 运行此脚本前需要按需调整CH_NO、APP_NAME、BUILD_MODE的值
BUILD_MODE=debug
CH_NO = ch03
APP_NAME = lseeklargetfile

# 获取makefile路径
PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
$(info MAKEFILE_LIST is: $(MAKEFILE_LIST))
$(info PROJECT_ROOT is: $(PROJECT_ROOT))

# 拼接路径
SOURCE_FRAGMENT :=$(CH_NO)/$(APP_NAME)
SOURCE_ROOT := $(PROJECT_ROOT)$(SOURCE_FRAGMENT)
$(info SOURCE_ROOT is: $(SOURCE_ROOT))
BUILD_ROOT := $(PROJECT_ROOT)build/make.debug.linux.x86_64/$(SOURCE_FRAGMENT)/
$(info BUILD_ROOT is: $(BUILD_ROOT))

ifeq ($(BUILD_MODE),debug)
	CFLAGS += -g
else ifeq ($(BUILD_MODE),run)
	CFLAGS += -O2
else
	$(error Build mode $(BUILD_MODE) not supported by this Makefile)
endif

$(shell if [ ! -d $(BUILD_ROOT) ]; then mkdir -p $(BUILD_ROOT); fi;)

SOURCE := $(shell find $(SOURCE_ROOT) -name '*.c' |xargs -n1 basename)
OBJS := $(SOURCE:.c=.o)
$(info SOURCE is: $(SOURCE))
$(info OBJS is: $(OBJS))

all: $(APP_NAME)
	
#
$(APP_NAME):	$(OBJS)
	$(CXX) -o $(BUILD_ROOT)/$@ $(addprefix $(BUILD_ROOT), $^)

%.o:	$(SOURCE_ROOT)/%.c
	$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $(BUILD_ROOT)/$@ $<

clean:
	rm -fr $(BUILD_ROOT)
